import * as types from './mutation-types'
import multiChannel from '../../../../api/config/channel/multi-channel'
import * as globalTypes from '../../../../mutation-types'
import Vue from 'vue'

/**
 * 获取邮箱开通状态并且获取邮箱list并且初始化
 * @param {Object} data
 */
export const getMailStatus = ({commit, dispatch}, data) => {
  return multiChannel.emailConfigEnter(data)
    .then(response => {
      if (response.success) {
        let p1 = dispatch('getCache', {type: 'mailQueues'})
        let p2 = dispatch('getCache', {type: 'emailSign'})
        Promise.all([p1, p2]).then((mailQueues) => {
          let quess = mailQueues[0]
          if (response.data && response.data.emailRelate) {
            response.data.emailRelate.forEach((item, index) => {
              for (let i = 0; i < quess.length; i++) {
                if (quess[i].Exten === item.skillGroup) {
                  Vue.set(response.data.emailRelate[index], 'skillGroupName', quess[i].DisplayName)
                }
              }
              if (!item.sign) {
                Vue.set(response.data.emailRelate[index], 'sign', '')
              }
            })
          }
          commit(types.SET_EMAIL_INFO, response)
        })
        return response
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 获取邮箱list
 * @param {Object} data
 */
export const emailConfigEnter = ({commit, dispatch}, data) => {
  return multiChannel.emailConfigEnter(data).then(response => {
    return response
  })
}
/**
 * 开通7m邮箱
 * @param {Object} data
 * @param {Object} data.name  需开通的邮箱
 */
export const emailServerOpen = ({commit}, data) => {
  return multiChannel.emailServerOpen(data)
    .then(response => {
      if (response.success) {
        commit(types.SET_EMAIL_STATUS, 'open')
        commit(globalTypes.SET_SUCCESS, 'message.empower_success')
      } else {
        commit(types.SET_EMAIL_STATUS, 'close')
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查看企业邮箱状态
 * @param data
 * @param data.to  企业邮箱
 * @param data.mailId  企业邮箱Id
 * @param data.name  代理邮箱名称
 * @returns {*}
 */
export const checkRelationEmail = ({commit}, data) => {
  return multiChannel.checkRelationEmail(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 发送验证邮件到邮箱
 * @param data
 * @param data.to  企业邮箱
 * @param data.mailId  企业邮箱Id
 * @param data.name  代理邮箱名称
 * @returns {*}
 */
export const sendCheckEmail = ({commit}, data) => {
  return multiChannel.sendCheckEmail(data)
    .then(response => {
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 删除企业邮箱
 * @param data
 * @param data.delID  企业邮箱Id
 * @returns {*}
 */
export const delRelationEmail = ({commit}, data) => {
  return multiChannel.delRelationEmail(data)
    .then(response => {
      if (response.success) {
        commit(types.DELETE_FIRM_MAIL, data)
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 添加或修改邮箱
 * @param data
 * @param data.emailRelskillGroup  企业邮箱技能组ID
 * @param data.relationEmailArrId  企业邮箱id
 * @param data.relationMailId  企业邮箱名称
 * @returns {*}
 */
export const addMailConfig = ({commit}, data) => {
  return multiChannel.addMailConfig(data)
    .then(response => {
      if (response.success) {
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 更改邮箱信息
 * @param data
 * @param data.emailRelskillGroup  企业邮箱技能组ID
 * @param data.relationEmailArrId  企业邮箱id
 * @param data.relationMailId  企业邮箱名称
 * @returns {*}
 */
export const updateMailConfig = ({commit}, data) => {
  return multiChannel.updateMailConfig(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, 'config.channel.saveSuccess')
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 获取邮箱分类列表
 * @param data
 * @param data.relateMailId  企业邮箱ID
 * @param data.page  获取页数
 * @param data.limit  获取条数
 * @param data.email  企业邮箱地址
 * @param data.flag  云客服类型  'kf'  和cc做区分
 * @returns {*}
 */
export const showMailCategory = ({commit}, data) => {
  return multiChannel.showMailCategory(data)
    .then(response => {
      if (response.success) {
        commit(types.UPDATE_MAIL_CATEGORY, response)
      }
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 获取邮箱分类详情
 * @param data
 * @param data.categoryId  分类id
 * @returns {*}
 */
export const editMailCategory = ({commit}, data) => {
  return multiChannel.editMailCategory(data)
    .then(response => {
      if (response.success) {
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 修改或保存分类
 * @param data
 * @param data.categoryId  分类id
 * @returns {*}
 */
export const saveAndUpdateMailCategory = ({commit}, data) => {
  return multiChannel.saveAndUpdateMailCategory(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, 'config.channel.saveSuccess')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 启用/停用分类标签
 * @param data
 * @param data._id  id
 * @param data.name  名称
 * @param data.content  内容
 */
export const switchMailCategoryEnable = ({commit}, data) => {
  return multiChannel.switchMailCategoryEnable(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, 'config.channel.saveSuccess')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 获取邮件的字典数据
 * @param data
 * @param data.type  需要获取的类型 'email'
 * @returns {*}
 */
export const getDicByType = ({commit}, data) => {
  return multiChannel.getDicByType(data)
    .then(response => {
      if (response.success) {
        // commit(globalTypes.SET_SUCCESS, 'config.channel.saveSuccess')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 删除邮件的结束类型
 * @param data
 * @param data.key  结束类型的key值
 * @param data.leavel  等级
 * @param data._id  等级
 * @returns {*}
 */
export const delDicItem = ({commit}, data) => {
  return multiChannel.delDicItem(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, 'message.deleteSucc')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 保存邮件的结束类型
 * @param data
 * @param data.key  结束类型的key值
 * @param data.leavel  等级
 * @param data._id  等级
 * @returns {*}
 */
export const saveDicItem = ({commit}, data) => {
  return multiChannel.saveDicItem(data)
    .then(response => {
      if (response.success) {
        // commit(globalTypes.SET_SUCCESS, '添加成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}

export const saveRedirectDic = ({commit}, data) => {
  return multiChannel.saveRedirectDic(data)
    .then(response => {
      if (response.success) {
        // commit(globalTypes.SET_SUCCESS, '添加成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 查询签名列表
 * @param data
 * @param data.page  页数
 * @param data.limit  条数
 */
export const querySignList = ({commit}, data) => {
  return multiChannel.querySignList(data)
    .then(response => {
      if (response.success) {
        // commit(globalTypes.SET_SUCCESS, '添加成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 删除邮件签名
 * @param data
 * @param data.page  页数
 * @param data.limit  条数
 */
export const deleteEmailSign = ({commit}, data) => {
  return multiChannel.deleteEmailSign(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, '删除成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 更新邮件签名
 * @param data
 * @param data._id  id
 * @param data.name  名称
 * @param data.content  内容
 */
export const updateEmailSign = ({commit}, data) => {
  return multiChannel.updateEmailSign(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, '设置邮件签名成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 保存邮件签名
 * @param data
 * @param data._id  id
 * @param data.name  名称
 * @param data.content  内容
 */
export const saveEmailSign = ({commit}, data) => {
  return multiChannel.saveEmailSign(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, '增加邮件签名成功')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 * 保存邮件签名
 * @param data
 * @param data._id  id
 * @param data.name  名称
 * @param data.content  内容
 */
export const saveRelationEmailConfig = ({commit}, data) => {
  return multiChannel.saveRelationEmailConfig(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, response.message)
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/*
* 添加专属座席
*/
export const saveFirmExclusiveSeat = ({commit}, data) => {
  return multiChannel.saveFirmExclusiveSeat(data)
    .then(response => {
      if (response.success) {
        commit(globalTypes.SET_SUCCESS, 'config.channel.saveSuccess')
      }
      return response
    })
    .catch(() => {
      commit(globalTypes.SET_ERROR, 'message.default_tips')
    })
}
/**
 *获取专属座席列表
 */
export const getExclusiveSeatList = ({commit}, data) => {
  return multiChannel.getExclusiveSeatList(data)
    .then(response => {
      return response
    })
}
/*
* 删除专属座席
* data:{_id: ''}
* */
export const deleteFirmExclusiveSeat = ({commit}, data) => {
  return multiChannel.deleteFirmExclusiveSeat(data)
    .then(response => {
      console.log(response)
      return response
    })
}

/**
 * 保存邮件满意度
 */
export const saveEmailCSRDic = ({commit}, data) => {
  return multiChannel.saveEmailCSRDic(data)
    .then(response => {
      return response
    })
}
